  /**
   * 文章服务类
   */
  var ArticleService = {
      /**
       * 获取文章点赞数量
       */
      getZanCount: function (id) {

          App.ajaxJson('/api/services/app/Article/GetZanCount', {
              key: id,
              ComposeType: 0
          }, (resData) => {
              if (!isNaN(resData)) {
                  $('#zan-article__count').text(resData);
                  if (resData == 0) {
                      this.setZan(false);
                  }
              }
          });
      },
      zan: function (callback) {
          App.ajaxJson('/api/services/app/Article/Zan', {
              "key": store.get('articleId'),
              "composeType": 0
          }, (resData) => {
              (!isNaN(resData)) && $('#zan-article__count').text(resData);
              if (callback instanceof Function) {
                  callback();
              }
          }, {}, 'post');

      },
      onZan: function () {
          $('#zan-article__i').on('click', (e) => {
              if (ArticleService.zaning) return;
              ArticleService.zaning = true;
              let userZan = store.get('userZan');

              let userZan_ = !userZan;
              this.setZan(userZan_);
              if (userZan_) {
                  //+
                  let zanCount = parseInt($('#zan-article__count').text());
                  $('#zan-article__count').text(zanCount + 1);
              } else {
                  //-
                  let zanCount = parseInt($('#zan-article__count').text());
                  $('#zan-article__count').text(zanCount - 1);
              }

              ArticleService.zan(() => {
                  store.set('userZan', !userZan);
                  ArticleService.zaning = false;
              });

          });
      },
      setZan: function (isZan) {
          if (isZan) {
              $('#zan-article__i').removeClass('fa-thumbs-o-up').addClass('fa-thumbs-up');
          } else {
              $('#zan-article__i').removeClass('fa-thumbs-up').addClass('fa-thumbs-o-up');
          }
      },
      zaning: false,
      getComments: function (articleId) {
          $('.comment-loading').show();
          App.ajaxJson('/api/services/app/Article/GetComments', {
              articleId: articleId,
              recursiveCommentReply: true,
              maxResultCount: 100,
              skipCount: 0
          }, (resData) => {
            $('.comment-loading').hide();
              let c_ul = $('#comment');
              c_ul.empty();
              if (resData && resData.items && resData.items.length) {
                  resData.items.forEach(m => {
                      let li = `
                      <div href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg">
                            <div class="weui-media-box__hd">
                                <img class="weui-media-box__thumb" src="${!!m.userPicture?'data:image/jpeg;base64,'+m.userPicture:store.get('baseUrl')+'/static/assets/images/user-head.png'}">
                            </div>
                            <div class="weui-media-box__bd">
                                <div class="weui-media-box__title">
                                    <span class="pull-left">${m.fromUserName}</span>
                                    <small class="pull-right">${App.formatDate(m.date,'/')}</small>
                                </div>
                                <p class="weui-media-box__desc">${m.content}</p>
                            </div>
                    </div>                   
                    `;
                      c_ul.append(li)
                  });
              }
          })
      },
      /**
       * 提交评论
       */
      submitComment: function (content) {
          let that = this;
          App.ajaxJson('/api/services/app/Article/CommentArticle', {
              "articleId": store.get('articleId'),
              "content": content
          }, function (resData) {
              that.getComments(store.get('articleId'));
              that.showAlert('success');
              $('#comment-w-input').val('');
          }, {}, 'post');
      },
      onSubmitComment: function () {
          let btn = $('#submit-comment');
          let that = this;
          btn.on('click', (e) => {

              let commentContent = $('#comment-w-input').val();
              if (!commentContent) {
                  that.showAlert('warning');
              } else {

                  that.submitComment(commentContent);
              }
          });
      },
      showAlert: function (type) {
          $('.alert-box.' + type).slideDown(function () {
              setTimeout(() => {
                  $('.alert-box.' + type).slideUp();
              }, 500);
          });
      },
      init: function () {
          this.getZanCount(store.get('articleId'));
          this.onZan();
          this.onSubmitComment();
          this.getComments(store.get('articleId'));
      }
  };

  ArticleService.init();